<template>
  <div class="admin-layout">
    <el-container>
      <!-- Sidebar -->
      <el-aside width="250px" class="sidebar">
        <div class="logo">
          <h2>Admin System</h2>
        </div>
        
        <el-menu
          :default-active="$route.path"
          class="sidebar-menu"
          router
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/">
            <el-icon><Dashboard /></el-icon>
            <span>Dashboard</span>
          </el-menu-item>
          
          <el-menu-item index="/users">
            <el-icon><User /></el-icon>
            <span>User Management</span>
          </el-menu-item>
          
          <el-menu-item index="/roles">
            <el-icon><UserFilled /></el-icon>
            <span>Role Management</span>
          </el-menu-item>
          
          <el-menu-item index="/permissions">
            <el-icon><Lock /></el-icon>
            <span>Permissions</span>
          </el-menu-item>
          
          <el-menu-item index="/modules">
            <el-icon><Grid /></el-icon>
            <span>Modules</span>
          </el-menu-item>
          
          <el-menu-item index="/content">
            <el-icon><Edit /></el-icon>
            <span>Content Management</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- Main Content -->
      <el-container>
        <!-- Header -->
        <el-header class="header">
          <div class="header-left">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/' }">Home</el-breadcrumb-item>
              <el-breadcrumb-item v-if="$route.name !== 'Dashboard'">
                {{ $route.name }}
              </el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          
          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="user-dropdown">
                <el-avatar :size="32" :src="avatarUrl">
                  {{ userInitials }}
                </el-avatar>
                <span class="username">{{ authStore.user?.fullName || authStore.user?.username }}</span>
                <el-icon><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">Profile</el-dropdown-item>
                  <el-dropdown-item command="settings">Settings</el-dropdown-item>
                  <el-dropdown-item divided command="logout">Logout</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- Main Content Area -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  Dashboard,
  User,
  UserFilled,
  Lock,
  Grid,
  Edit,
  ArrowDown
} from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()

const avatarUrl = computed(() => {
  // You can implement avatar URL logic here
  return ''
})

const userInitials = computed(() => {
  const user = authStore.user
  if (user?.fullName) {
    return user.fullName.split(' ').map(n => n[0]).join('').toUpperCase()
  }
  return user?.username?.substring(0, 2).toUpperCase() || 'U'
})

const handleCommand = (command: string) => {
  switch (command) {
    case 'profile':
      ElMessage.info('Profile page - to be implemented')
      break
    case 'settings':
      ElMessage.info('Settings page - to be implemented')
      break
    case 'logout':
      authStore.logout()
      router.push('/login')
      ElMessage.success('Logged out successfully')
      break
  }
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  overflow: hidden;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #2b3a4b;
  color: white;
  margin-bottom: 0;
}

.logo h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.sidebar-menu {
  border: none;
  height: calc(100vh - 60px);
}

.sidebar-menu .el-menu-item {
  height: 50px;
  line-height: 50px;
}

.header {
  background-color: white;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  flex: 1;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-dropdown:hover {
  background-color: #f5f5f5;
}

.username {
  margin: 0 8px;
  font-size: 14px;
  color: #333;
}

.main-content {
  background-color: #f0f2f5;
  padding: 20px;
  overflow-y: auto;
}
</style>
